package com.多态.基础;
class Animal{
    String name;
    Animal(){

    }
    Animal(String name){
        this.name = name;
    }

    void fun1(Animal animal){
        animal.food();
    }
    Animal fun2(String name){
        Dog dog = new Dog(name);
        return dog;
    }
    Dog fun3(String name){
        return new Dog(name);
    }
    void food(){
        System.out.println(this.name+"吃饭");
    }
    void habit1(){
        System.out.println(this.name+"爱好");
    }
}
class Dog extends Animal{
    Dog(){
      super();
    }
    Dog(String name){
        super(name);
    }

    @Override
    void food() {
        System.out.println(name+"吃骨头");
    }

    @Override
    void habit1() {
        System.out.println(name+"喜欢摇尾巴");
    }
    void habit3(){
        System.out.println(name +"喜欢玩");
    }
}
public class 向上转型 {
    public static void main(String[] args) {
        //1.直接赋值
        Animal animal =new Dog("大黄");
        animal.habit1();
        Dog dog = new Dog("小黄");
        Animal animal1 = dog;
        animal1.food();


        //2.方法传参方式
        Dog dog1 = new Dog("大黑");
        dog1.fun1(dog1);

        //3.返回值
        Dog dog2 =new Dog();
        Animal animal2 = dog2.fun2("hh");

        //向上转型缺点
        //无法调用子类方法，只能调用父类的方法
        Animal animal3 =new Dog("嘿嘿");
//        animal3.habit3();//报错


        //调用子类，运行时绑定或者动态绑定
        animal3.food();//构成重写，调用子类的方法
        //在运行时发现，子类重写父类的方法，所以会调用子类的方法
        //与之对应的是，编译时绑定或者静态绑定：方法的重载

    }
}
